<?php
include 'redirectAdmin.php';
$action = "find";
if (isset ( $_GET ["action"] )) {
 $action = $_GET ["action"];
}
?>

<table class="manage-ticket">
	<tr>
  		<td class="left-menu">
   			<div><a href="bookmarks.php?page=manageTicket&action=find">Odszukaj</a></div>  
  		</td>
  		<td>
		  	<?php 
		  	if ($action =="find" ){
		  		$ticketControls = new TicketSearchControls();
		  		if("true" == ParamUtils::get("send")){
		  			$ticketControls->setActive(ParamUtils::getBoolean("active"));
		  			$ticketControls->setAssignSu(ParamUtils::getBoolean("hasUser"));
		  		}
		  		$usid=null;
		  		if(($usid = ParamUtils::get("usid")) != null){
		  			$ticketControls->setUserServiceId("usid");
		  		}
		  		$tickets = TicketUtils::find($ticketControls);
		  	
		  	?>
			  	<form method="post" action="bookmarks.php?page=manageTicket&action=find">
				  	Aktywne <input type="checkbox" name="active" <?php echo ParamUtils::getBoolean("active") ? "CHECKED" : "";?>/>
				  	Posiadjace super uzytkownika <input type="checkbox" name="hasUser" <?php echo ParamUtils::getBoolean("hasUser") ? "CHECKED" : "";?>/>
				  	<input type="submit" value="szukaj"/>
				  	<input type="hidden" name="send" value="true"/>
			  	</form>
			  	
			  	<table>
				  	<tr>
					     <th>Super User</th>
					     <th>Zglaszajacy</th>
					     <th>Opis</th>
					     <th></th>
			    	</tr>
		  	<?php 
		  		foreach($tickets as $ticket){
		  			$superUserName = "brak";
		  			if($ticket->getSuperUser() != null){
		  					$su = UserUtils::get($ticket->getSuperUser());
		  					$superUserName = $su->getName(). " " . $su->getSurname();
		  			}
		  			$userName = UserServiceUtils::get($ticket->getUsid())->getFullUserName();
		  			$descs = $ticket->getDescriptions();
		  			 echo "<tr> 
		         			<td>". $superUserName ." </td>
			        	 	<td>" . $userName . "</td>
			         		<td>" .  $descs[0]->get()  . " </td>
			         		<td> <a href='bookmarks.php?page=manageTicket&action=manage&tid=" . $ticket->getId() . "'>Zarządzaj</a></td>
		        		</tr>";
		        		
		  		}
		  	
		  	
		  	?>
		  		</table>
		  	<?php } else if(ParamUtils::get("tid")!=null){//zarzadzanie ticketem
		  		$ticket = TicketUtils::get(ParamUtils::get("tid"));
		  		
		  		$descs = $ticket->getDescriptions();
		  		$suid = null;
		  		if(($suid = ParamUtils::get("supUs")) != null){
		  			TicketUtils::assignSuperUserById($ticket->getId(), $suid);
		  			$ticket->setSuperUser($suid);
		  		}
		  		
		  		$answer = null;
		  		if(($answer = ParamUtils::get("answer")) != null){
		  			$ticketAnswer = new TicketDescription();
		  			$ticketAnswer->setTid($ticket->getId());
		  			$ticketAnswer->setUserId($_SESSION['curUser']->getId());
		  			$ticketAnswer->set($answer);
		  			TicketUtils::addDescription($ticketAnswer);
		  			$descs[] = $ticketAnswer;
		  		}
		  		?>
			
			
				<form action="bookmarks.php?page=manageTicket&action=manage&tid=<?php echo $ticket->getId(); ?>" method="post" > 	 	
			  	<?php 
			  			if($_SESSION['curUser']->getRole() == UserRoles::ADMIN_ROLE && $ticket->getSuperUser() == null && $ticket->isActive()){
			  				$controls = new UserSearchControls();
			  				$controls->setRole(UserRoles::SUPER_USER_ROLE);
			  				$superUsers = UserUtils::find($controls);
			  	?>
			  			<select name="supUs">
			  				<?php foreach($superUsers as $superUser) {
			  					echo "<option value=" . $superUser->getId() . ">". $superUser->getName() . " " . $superUser->getSurname() . " </option>";
			  				}?>
			  			</select>
			  			<input type="submit" value="Przypisz" /> 
			  	<?php 	} else { 
			  				if($ticket->isActive()){
			  					$superUser = UserUtils::get($ticket->getSuperUser());
			  					echo "<div> Super użytkownik przypisany do zgłoszenia " . $superUser->getName() . " " . $superUser->getSurname() . "</div>";
			  				}  
			  			}
			  	?>  	
			  	
			  		<table>
				  		<?php foreach($descs as $desc){
				  				$userWhoWrite = UserUtils::get($desc->getUserId());
				  				echo "<tr> 
				  							<td> " .  $userWhoWrite->getName() . " " . $userWhoWrite->getSurName() ." </td>
				  					 </tr>
				  				   	 <tr>
				  				   	 		<td>
											<textarea readonly='readonly' rows='10' cols='20'>"  .  stripslashes($desc->get()) . " </textarea>
											</td>
				  				   	 </tr>
				  				   	 <tr>
				  					 	<td> <hr /> </td>
				  					 </tr>
				  				   	 ";	
				  			}
				  		
				  			
							if($_SESSION['curUser']->getId() == $ticket->getSuperUser()){
										
								echo "<tr> 
										<td> <textarea name='answer' rows='10' cols='20'></textarea></td> 
									  </tr>
									  <tr> 
										<td> <input type='submit' value='Odpowiedz'/></td> 
									  </tr>";
							}
				  		?>
				  	</table>
		 	 	</form>
			<?php }?>
		  </td>
 </tr>
</table>